//
//  CADataModel.m
//  cascade-realm
//
//  Created by QSC on 22/5/24.
//

#import "CADataModel.h"

@interface CADataModel()
@property(nonatomic,strong)CAHomeUser *user;
@end

@implementation CADataModel

+ (NSDictionary *)mj_objectClassInArray
{

    return @{@"commentArray" : @"CAComment"};

}

- (NSArray *)allImages{
    NSMutableArray * arr = [NSMutableArray arrayWithArray:self.images];
    [arr insertObject:self.cover atIndex:0];
    return arr;
}




+ (void)getHomeDataModels:(void(^)(NSArray *))success{
    

    NSDictionary *dataDic = [self getDataFormloacl];
    
    NSMutableArray * keys = [NSMutableArray arrayWithArray:dataDic.allKeys];
 
    NSMutableArray * result = [NSMutableArray array];
    
    
    [Tool ShowLoading];
    
    [CAHomeUser getHomeUsers:^(NSArray * _Nonnull array) {
        
        for (CAHomeUser * user in array) {
        
            if (user.dataKeys.count == 0) {
                NSArray * arr = [CADataModel mj_objectArrayWithKeyValuesArray:dataDic[keys.firstObject]];
                NSMutableArray * dataKeys = [NSMutableArray array];
                for (CADataModel * model in arr) {
                    [dataKeys addObject:model.key];
                    
                    
                    model.binduserId = user.userId;
                    [result addObject:model];
                    //保存详情
                    [DBTool saveData:model.mj_JSONObject tag:model.key];
                }
                user.dataKeys = dataKeys;
                
                [keys removeFirstObject];
                //赋值后重新保存用户
                [DBTool saveData:user.mj_JSONObject tag:user.userId];
            }else{
                
                for (NSString * key in user.dataKeys) {
                    
                    NSDictionary * dic = [DBTool getData:key];
                    CADataModel *model = [CADataModel mj_objectWithKeyValues:dic];
                    [result addObject:model];
                }
                
                
            }
        }
        
        
     
        //打乱一下顺序
        [result randomObjects];
   
        success(result);
        
        [Tool HideLoading];
        
    } waitTime:1];
    
    
    
}

+ (NSDictionary * )getDataFormloacl{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    return  dataDic;
}


- (CAHomeUser *)getUser{
    

    NSDictionary *dic = [DBTool getData: self.binduserId];
    self.user = [CAHomeUser mj_objectWithKeyValues:dic];
    
    return self.user;
}


- (void)save{
    
 
    [DBTool saveData:self.mj_keyValues tag:self.key];
    
}


//不能重写set，要重新定义个方法
- (void)SetIslikeInDB:(BOOL)islike{
    NSString * k = [NSString stringWithFormat:@"%@--islike",CAUserInfo.shared.userInfo.userId];
    NSDictionary * dic = [DBTool getData:k];
    
    NSArray * arr = dic[@"islike"];
    NSMutableArray * array = [NSMutableArray array];
    if (arr != nil) {
        array = [NSMutableArray arrayWithArray:arr];
    }
    
    if (islike) {
        [array addObject:self.key];
    }else{
        [array removeObject:self.key];
    }

    [DBTool saveData:@{@"islike":array} tag:k];
}



- (BOOL)islike{
    NSString * k = [NSString stringWithFormat:@"%@--islike",CAUserInfo.shared.userInfo.userId];
    NSDictionary * dic = [DBTool getData:k];
    
    NSArray * arr = dic[@"islike"];
    
    if (arr != nil && [arr containsObject:self.key]) {
        return YES;
    }
    
    
    return NO;
}

- (void)addToPublishArticle{
    
    NSDictionary * dic = [DBTool getData:[NSString stringWithFormat:@"publishArticle--%@",CAUserInfo.shared.userInfo.userId]];
    NSMutableDictionary * result = [NSMutableDictionary dictionaryWithDictionary:dic];
    NSArray * ids = result[@"ids"];
    NSMutableArray * idArray = [NSMutableArray array];
    if (ids != nil) {
        idArray = [NSMutableArray arrayWithArray:ids];
    }
    [idArray addObject:self.key];
    result[@"ids"] = idArray;
    [DBTool saveData:result tag:[NSString stringWithFormat:@"publishArticle--%@",CAUserInfo.shared.userInfo.userId]];
    
}

- (void)removeFormPublishArticle{
    NSDictionary * dic = [DBTool getData:[NSString stringWithFormat:@"publishArticle--%@",CAUserInfo.shared.userInfo.userId]];
    NSMutableDictionary * result = [NSMutableDictionary dictionaryWithDictionary:dic];
    NSArray * ids = result[@"ids"];
    NSMutableArray * idArray = [NSMutableArray array];
    if (ids != nil) {
        idArray = [NSMutableArray arrayWithArray:ids];
    }
    [idArray removeObject:self.key];
    result[@"ids"] = idArray;
    [DBTool saveData:result tag:[NSString stringWithFormat:@"publishArticle--%@",CAUserInfo.shared.userInfo.userId]];
}

+ (NSMutableArray *)getPuhlishAtticle{
    NSDictionary * dic = [DBTool getData:[NSString stringWithFormat:@"publishArticle--%@",CAUserInfo.shared.userInfo.userId]];
    NSArray * ids = dic[@"ids"];
    if (ids == nil) {
        return [NSMutableArray array];
    }
    NSMutableArray * resultArray = [NSMutableArray array];
    
    for (NSString * key in ids) {
        NSDictionary * data = [DBTool getData:key];
        CADataModel * model = [CADataModel mj_objectWithKeyValues:data];
        [resultArray addObject:model];
    }
    
    return  resultArray;
    
}


- (void)addToPublishForum{
    
    NSDictionary * dic = [DBTool getData:[NSString stringWithFormat:@"publishForum--%@",CAUserInfo.shared.userInfo.userId]];
    NSMutableDictionary * result = [NSMutableDictionary dictionaryWithDictionary:dic];
    NSArray * ids = result[@"ids"];
    NSMutableArray * idArray = [NSMutableArray array];
    if (ids != nil) {
        idArray = [NSMutableArray arrayWithArray:ids];
    }
    [idArray addObject:self.key];
    result[@"ids"] = idArray;
    [DBTool saveData:result tag:[NSString stringWithFormat:@"publishForum--%@",CAUserInfo.shared.userInfo.userId]];
    
}


+ (NSMutableArray *)getPuhlishForum{
    NSDictionary * dic = [DBTool getData:[NSString stringWithFormat:@"publishForum--%@",CAUserInfo.shared.userInfo.userId]];
    NSArray * ids = dic[@"ids"];
    if (ids == nil) {
        return [NSMutableArray array];
    }
    NSMutableArray * resultArray = [NSMutableArray array];
    
    for (NSString * key in ids) {
        NSDictionary * data = [DBTool getData:key];
        CADataModel * model = [CADataModel mj_objectWithKeyValues:data];
        [resultArray addObject:model];
    }
    
    return  resultArray;
    
}


@end



@implementation CAComment

@end
